package com.base.string;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

/**
 * 38. 外观数列
 * 描述： https://leetcode-cn.com/problems/count-and-say/
 * @author leon
 * @date 2021年10月15日 21:39
 */
public class CountAndSay {
    public String countAndSay(int n) {
        if (n==1){
            return "1";
        }
       String s = "1";
        for (int i = 2; i <= n; i++) {
            s =countAndSay(s);
        }

        return s;
    }

    public String countAndSay(String s){
        char pre =  s.charAt(0);
        int size = 1;
        StringBuilder res =new StringBuilder();
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i)==s.charAt(i-1)){
                size+=1;
            }else {
                res.append(size).append(pre);
                pre =  s.charAt(i);
                size = 1;
            }
        }
        res.append(size).append(pre);
        return res.toString();
    }

}
